package breeze.pixel.weather.share.view;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.core.content.FileProvider;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import breeze.pixel.weather.BaseView;
import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.utils.AppToolUtil;
import breeze.pixel.weather.share.presenter.IShareWeatherPresenter;
import breeze.pixel.weather.share.presenter.ShareWeatherPresenter;

public class ShareWeatherView extends BaseView implements IShareWeatherView{

    private ImageView img_share;
    private Bitmap bmp;

    @Override
    public void init() {
        img_share=find(R.id.share_img);
    }

    @Override
    public void initData() {
        View view = LayoutInflater.from(this).inflate(R.layout.share_view,null);
        IShareWeatherPresenter presenter = new ShareWeatherPresenter(this);
        //初始化视图
        Bundle data = getIntent().getExtras();
        presenter.layout(data,view);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        setContentView(R.layout.activity_share);
        init();
        initData();
    }

    public void save(View view){
        if(bmp!=null){
            if(checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                try {
                    AppToolUtil.insertPicture(this, bmp);
                    toast("保存成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    toast("保存失败：" + e.toString());
                }
            }
        }
    }

    public void share(View view){
        if(bmp!=null){
            String file_path= AppToolUtil.getCacheFile(this, AppToolUtil._PATH_BING_PIC_CACHE_DIR)+AppToolUtil.getSimpleDate("yyyy-MM-dd HH-mm")+".jpg";
            try {
                bmp.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file_path));
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_SEND);
                Uri img_uri;
                if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
                    img_uri=FileProvider.getUriForFile(this, getFileProvider(), new File(file_path));
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                }else{
                    img_uri=Uri.fromFile(new File(file_path));
                }
                intent.putExtra(Intent.EXTRA_STREAM, img_uri);
                intent.setType("image/*");
                startActivity(Intent.createChooser(intent, "分享图片"));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                toast("分享失败："+e.toString());
            }

        }
    }

    @Override
    public void setPicView(Bitmap view) {
        img_share.post(() -> {
            bmp = view;
            img_share.setImageBitmap(view);
        });
    }
}
